Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save willwhui/7cd33c82853fc0d9a44852c3724842c5 to your computer and use it in GitHub Desktop.
Save willwhui/7cd33c82853fc0d9a44852c3724842c5 to your computer and use it in GitHub Desktop.
在haas on Raspberry 上设置一个random sensor触发的alert及automation
@willwhui
Copy link
Author

willwhui commented Oct 27, 2017

action中的普通service

参见https://home-assistant.io/docs/automation/action/
及其中提到的The action part follows the script syntax

调用规则:貌似比较简单?

automation:
# Turns on lights 1 hour before sunset if people are home
# and if people get home between 16:00-23:00
  - alias: 'Rule 1 Light on in the evening'
    trigger:
      (略)
    condition:
      (略)
    action:
      service: homeassistant.turn_on    <---- service-domain.service-name
      entity_id: group.living_room      <----- 影响的entiy

# Turn off lights when everybody leaves the house
  - alias: 'Rule 2 - Away Mode'
    trigger:
      (略)
    condition:
      (略)
    action:
      service: notify.notify    <---- service-domain.service-name,notify类型的service没有必要指定entity_id
      data:     <----- 这是个需要接受的数据,最终会被封装成json
        message: 'Paulus left the house'   <------ 一个或多个key-value

问题:

  • service domain和service name看起来就是在前端界面的service页面中查看 http://ip:8123/dev-service
  • 注册服务的机制还不太清楚,比如alert, notify自动注册了服务
  • 服务需要接受的数据定义,看起来是要到servie组件的说明里面去看

@willwhui
Copy link
Author

willwhui commented Oct 27, 2017

action中的特殊service:场景(scene)

参见:https://home-assistant.io/components/scene/

You can create scenes that capture the states you want certain entities to be.

不知道为什么要用'capture'这个单词,难道是美语俗语?
根据上下文的意思和实际用途,这句话是这个意思:
“你可以创建一些场景,将特定的实体(entities,虚拟的或者真实的设备)设定到指定的状态。
类似于”

You can create scenes that set up the states you want certain entities to be.

例子:

scene:
  - name: Romantic   <--------- 一个场景name
    entities:     <--------- 对应的实体们开始了
      light.tv_back_light: on    <--------- 单独设置此实体的state
      light.ceiling:        <--------- 设置此实体的多个状态
        state: on      <--------- state
        xy_color: [0.33, 0.66]  <-------- attribute
        brightness: 200  <-------- attribute

调用规则:貌似也很简单?

例子:

automation:
  trigger:
    (略)
  action:
    service: scene.turn_on   <------- 或者:scene.turn_off,其实也可以理解为 domain.name?这样也就不是特殊的东西了
    entity_id: scene.romantic <------- 影响的entity

@willwhui
Copy link
Author

willwhui commented Oct 27, 2017

用automation来触发twitter

- alias: 'Random sensor trigger twitter'
  trigger:
    platform: state
    entity_id: binary_sensor.random_change
    to: "on"
  action:
    service: notify.twitter_wang3an
    data:
      message: A random value less than 50 triggered this.

像上面这样写,只触发一次就没动静了,为什么呢?
看日志:

Oct 27 18:41:55 raspberrypi hass[941]: 2017-10-27 18:41:55 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.random_1, old_state=<state sensor.random_1=84; icon=mdi:hanger, friendly_name=random 1 @ 2017-10-27T18:41:24.308602+08:00>, new_state=<state sensor.random_1=27; icon=mdi:hanger, friendly_name=random 1 @ 2017-10-27T18:41:55.307233+08:00>>
^---- 上面这句是说:random值变了
Oct 27 18:41:55 raspberrypi hass[941]: 2017-10-27 18:41:55 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=binary_sensor.random_change, old_state=<state binary_sensor.random_change=off; friendly_name=A random value less than 50 @ 2017-10-27T18:38:18.315139+08:00>, new_state=<state binary_sensor.random_change=on; friendly_name=A random value less than 50 @ 2017-10-27T18:41:55.316416+08:00>>
^---- 上面这句说:binary_sensor.random_change的值变成on了(符合预期)
Oct 27 18:41:55 raspberrypi hass[941]: 2017-10-27 18:41:55 INFO (MainThread) [homeassistant.components.automation] Executing Random sensor trigger twitter
^---- 上面这句说:执行automation中(符合预期)
Oct 27 18:41:55 raspberrypi hass[941]: 2017-10-27 18:41:55 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: entity_id=automation.random_sensor_trigger_twitter, name=Random sensor trigger twitter, domain=automation, message=has been triggered>
^---- 写日志文件
Oct 27 18:41:55 raspberrypi hass[941]: 2017-10-27 18:41:55 INFO (MainThread) [homeassistant.helpers.script] Script Random sensor trigger twitter: Running script
^---- 运行scrip中
Oct 27 18:41:55 raspberrypi hass[941]: 2017-10-27 18:41:55 INFO (MainThread) [homeassistant.helpers.script] Script Random sensor trigger twitter: Executing step call service
^---- 可以call service了(符合预期)
Oct 27 18:41:55 raspberrypi hass[941]: 2017-10-27 18:41:55 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service=twitter_wang3an, domain=notify, service_data=message=A random value less than 50 triggered this., service_call_id=1971673456-3>
^---- bus处理事件:调用service=twitter_wang3an(符合预期)
Oct 27 18:41:56 raspberrypi hass[941]: 2017-10-27 18:41:56 ERROR (Thread-5) [homeassistant.components.notify.twitter] Error 403: [{'message': 'Status is a duplicate.', 'code': 187}]
^---- 返回错误:Status is a duplicate。莫非是twitter防止灌水用的?
Oct 27 18:41:56 raspberrypi hass[941]: 2017-10-27 18:41:56 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1971673456-3>
^---- 调用结束

信息里面有一个:
Error 403: [{'message': 'Status is a duplicate.', 'code': 187}]
原来是twitter干的:防止重复发送
参见:https://twittercommunity.com/t/status-is-a-duplicate/16928

修改一下触发后发送的内容:

- alias: 'Random sensor trigger twitter'
  trigger:
    platform: state
    entity_id: binary_sensor.random_change
    to: "on"
  action:
    service: notify.twitter_wang3an
    data:
      message: A random value less than 50 triggered this.

@willwhui
Copy link
Author

willwhui commented Nov 2, 2017

小坑

  • 你可以在alias的平级增加id: 1508639450841这样一行(数字任意,避免和现有id重复)
    这样就可以在hass的前端的configuration模块中,被automation模块识别列出了
    但是:
    一旦你对任何一个automaiton模块进行修改和保存,那么automations.xml中的所有内容都会被重写。
    功能虽正常,但注释全被销毁,语句顺序也发生一定的变化。
    所以还是别这样搞了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment