Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95919

Make "scripts" as toggle "switch"

$
0
0

@Erimus wrote:

Feature

  • I have 2 script session for “Theater mode on” and “off”. In the script is many steps.
  • If I add this 2 scripts in homekit or front-end, it shows 2 buttons.
    If you had “delay” in you script, the UI will show a toggle button instead of “execute”. In homekit, it alsways display as switch.
  • When you press it, the toggle will shows “on” during the script runs, but when the script finish, the button state turns “off”. The status can not keep.
  • My target is make the “Theater Mode” as single toggle button.
  • I’ve tried some other method, but a bit hard to understand. I found a method that easy understanding.
  • The main part of the method is create a fake switch as a listener, then when you toggle it, run a automation contain you script.
    In front-end, just need to display the fake switch button, and it can keep status.
  • Hope it to be helpful.

Script

  • In script, you need do nothing special, just add you functions as normal.
theater_mode_on:
  alias: 'ON'
  sequence:
    # 放下幕布
    - service: switch.turn_on
      data:
        entity_id: switch.screen
    - delay: 00:10:00
    # 打开投影仪
    - service: switch.turn_on
      data:
        entity_id: switch.projector_power
    - delay: 00:00:10
    # 关灯
    - service: switch.turn_off
      data:
        entity_id: group.light

theater_mode_off:
  alias: 'OFF'
  sequence:
    # 收起幕布
    - service: switch.turn_off
      data:
        entity_id: switch.screen
    - delay: 00:00:01
    # 关闭投影仪
    - service: switch.turn_off
      data:
        entity_id: switch.projector_power
    - delay: 00:00:03
    - service: switch.turn_off
      data:
        entity_id: switch.projector_power
    # 开灯
    - service: switch.turn_on
      data:
        entity_id: switch.light_2
    - delay: 00:00:01

Create fake switch

  • Create a fake switch as listener.
- platform: command_line
  switches:
    # theater mode ==============================
    theater_mode:
      friendly_name: Theater
      command_on: ''
      command_off: ''

Automations

  • Use automation to hook the switch status, and run scripts.
- id: 'theater_mode_on'
  alias: Theater mode ON
  description: 剧院模式开启
  trigger:
  - entity_id: switch.theater_mode
    platform: state
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: script.theater_mode_on
- id: 'theater_mode_off'
  alias: Theater mode OFF
  description: 剧院模式关闭
  trigger:
  - entity_id: switch.theater_mode
    platform: state
    from: 'on'
    to: 'off'
  condition: []
  action:
  - service: script.theater_mode_off

Finally, add to homekit.

filter:
  include_entities:
    # - script.theater_mode_on
    # - script.theater_mode_off
    - switch.theater_mode

  • This method is easier to understand for me.
  • But it’s not perfect, because there would be more code in automations. In other method, maybe template switch is enough, but I do not really understand it. (I’m not familiar with hass)
  • There are also some interesting useage, such as you can create a button called open aircon, then write some automation with conditions like temperature range, then control the temperature of aircon.
  • I’m new here, so I do not know how to write a “if…elif…” or “while…case…” in hass. Maybe my method looks fool, please tell me some better, I will be happy to learn.
  • PS: automation is still not good enough for me. For example, I back home, I wish some switch turn on, and in conditional by sun to toggle lights. I have to split the code in 2 automation, because the conditions work on all actions, it looks duplicate in most part. Is that some elegant way? Please tell me.

Posts: 16

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 95919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>