Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save willwhui/b4a365416881f3dc4a26bbeeea49ec7e to your computer and use it in GitHub Desktop.
Save willwhui/b4a365416881f3dc4a26bbeeea49ec7e to your computer and use it in GitHub Desktop.
利用人体感应器制作一个提醒休息的工具
@willwhui
Copy link
Author

willwhui commented Jan 8, 2018

目标1:

坐在感应器前会触发一个tts提醒。
然后人工告诉google home设置一个event
实现简单,但其弊端在于需要人呼叫google home,且google event更新并不及时。

# begin: "TTS for prompt to set a working timer"
- alias: "TTS for prompt to set a working timer: enable at start"
  #hide_entity: true
  trigger:
    - platform: homeassistant
      event: start # when all automation rules loaded by hass, this rule will then be triggerd.
  action:
    - service: automation.turn_on # turn off self
      entity_id: automation.tts_for_prompt_to_set_a_working_timer_trigger

- alias: "TTS for prompt to set a working timer: trigger"
  initial_state: false # to prevent the motion sensor trigger it at hass start before all rules loaded
  #hide_entity: true
  trigger:
    - platform: event
      event_type: motion
      event_data:
        entity_id: !secret motion_sensor_1_id
  condition:
    condition: state
    entity_id: calendar.start_work
    state: 'off' # if no calendar.start_work
  action:
    - service: automation.turn_off # turn off self
      entity_id: automation.tts_for_prompt_to_set_a_working_timer_trigger
    - service: automation.turn_on # turn on a timer triggered rule to enable this again
      entity_id: automation.tts_for_prompt_to_set_a_working_timer_enable_again
    - service: tts.google_say
      entity_id: media_player.living_room_home
      data:
        message: "Hey, man, you'd better setup a start work timer!"

- alias: "TTS for prompt to set a working timer: enable again"
  initial_state: false
  #hide_entity: true
  trigger:
      platform: time
      minutes: '/1' # shouldn't be too short, because calendar state updating takes time
      seconds: 00
  action:
    - service: automation.turn_off # turn self off
      entity_id: automation.tts_for_prompt_to_set_a_working_timer_enable_again
    - service: automation.turn_on # turn on the trigger again
      entity_id: automation.tts_for_prompt_to_set_a_working_timer_trigger
# end: "TTS for prompt to set a working timer"

@willwhui
Copy link
Author

willwhui commented Jan 8, 2018

目标2:难点在于找一个地方存储当前状态,以便于重启后能续上时间

看起来用input-number + recorder来做最靠谱

它们可以restore重启之前的数据
https://home-assistant.io/components/input_number/

RESTORE STATE
This component supports the restore_state function which restores the state after Home Assistant has started to the value it has been before Home Assistant stopped. To use this feature please make sure that the recorder component is enabled and your entity does not have a value set for initial. Additional information can be found in the Restore state section of the recorder component documentation.

https://home-assistant.io/components/recorder/

If the recorder component is activated then some components support restore_state which will restore the state of the entity after Home Assistant is started to the state before Home Assistant was stopped.

An incomplete list of components that currently support restore_state:
input_boolean
input_number
input_select
input_datetime
input_text

经过简单的测试,并不需要指明使用recorder,input_number就已经可以自动restore_state了

@willwhui
Copy link
Author

利用input_number实现计时器

以定时30分钟为例

  • 启动计时器时,将input_number设定为1
  • 启动一个automation,每隔1分钟将input_number+1
  • 启动一个automation,每隔1分钟检查一次,如果input_number=30,就发起TTS,并将input_number重置为0
  • 每当hass启动时,如果input_number != 0,则认为timer需要继续

@willwhui
Copy link
Author

示例

设置一个input_number作为计数器

实际使用的时候,每分钟+1

start_work_timer:
  name: 'Start work timer'
  # do not set a initial value, 
  # because this will cause it be initialized at every restart of hass.
  # without this setting, hass will reload the last value it was, or set to '0' if it is the very first time initializing for it
  # initial: 0 
  min: 0
  max: 100
  step: 1

注意,initial千万不要设置,否则每次重启的时候都会被设置为这个初始值。第一次启用这个number的时候,系统会将它设置为0

设置一个input_boolean作为flag

start_work:
  name: "Work"
  initial: off
  icon: mdi:worker

便于通过Alexa语音控制,也是一个显式的标记。
同时也是一个被automation触发器监视的状态点。

启动

- alias: "Set working timer by motion: trigger set flag up by motion"
  #hide_entity: true
  trigger:
    - platform: event
      event_type: motion
      event_data:
        entity_id: !secret motion_sensor_1_id
  condition:
    condition: and
    conditions:
    - condition: numeric_state
      entity_id: input_number.start_work_timer
      below: 1 # less than 1 means no working timer now
    - condition: state
      entity_id: automation.tts_for_take_a_walk_repeating
      state: 'off' # only set a timer while there is not a repeating tts current
  action:
    - service: input_boolean.turn_on # set flag up
      entity_id: input_boolean.start_work
  • alias: "Set working timer by motion: trun on"
    initial_state: true
    #hide_entity: true
    trigger:
    • platform: state
      entity_id: input_boolean.start_work
      to: 'on'
      condition:
      condition: and
      conditions:
    • condition: numeric_state
      entity_id: input_number.start_work_timer
      below: 1 # less than 1 means no working timer now
    • condition: state
      entity_id: automation.tts_for_take_a_walk_repeating
      state: 'off' # no repeating tts means no working timer now
      action:
    • service: input_number.set_value
      data:
      entity_id: input_number.start_work_timer
      value: 1 # inital start value
    • service: automation.turn_on # start counting
      entity_id: automation.set_working_timer_by_motion_counting
    • service: media_player.turn_on # prompt started
      entity_id: !secret current_media_player_id
    • delay: "00:00:03" # left some time for divice response
    • service: tts.google_say
      entity_id: !secret current_media_player_id
      data:
      message: "A start work timer started!"

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