HA script commands

Hi

I’m switching from domoticz to Home Assistant. The scripting is totally different and I can’t find anywhere the commands that you can use and the syntax for it. I’m trying a simple if then else statement for practicing but don’t have any idea how to do it. this is the test script I created in the visual editor. What is want is to create also the off state of the switch to turn on the lamp. I hope there is a good website with syntax and available commands.

alias: New Automation
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.test
    for:
      hours: 0
      minutes: 0
      seconds: 3
condition:
  - condition: state
    entity_id: input_boolean.test
    state: 'on'
action:
  - type: turn_off
    device_id: 20b995602s83et23b564926e3bf776d97cd
    entity_id: light.wand_lamp_1
    domain: light
mode: single
  1. By “switch” do you mean the boolean?
  2. If the boolean is “off” your condition will prevent the execution of the action
  3. Do you really want the boolean both changing to “on” and changing to “off” to turn on the lamp?

If I understand your post correctly, this should do what you described:


alias: New Automation
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.test
    for:
      hours: 0
      minutes: 0
      seconds: 3
condition: [] 
action:
  - type: turn_on
    device_id: 20b995602s83et23b564926e3bf776d97cd
    entity_id: light.wand_lamp_1
    domain: light
mode: single

thanks for the quick reply. I meant indeed the boolean. if it is on then the light should be on if the boolean is off then the light should be off.

Ok, there are a couple ways to do that.

If you are just starting out, and want to use the visual editor, you will probably want to use the Choose action and Trigger IDs.

alias: New Automation
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.test
    to: 'on'
    for: '00:00:03'
    id: On
  - platform: state
    entity_id: input_boolean.test
    to: 'off'
    for: '00:00:03'
    id: Off
condition: [] 
action:
  - choose:
    - conditions:
        condition: trigger
        id: Off
      sequence:
        - type: turn_off
          device_id: 20b995602s83et23b564926e3bf776d97cd
          entity_id: light.wand_lamp_1
          domain: light
    - conditions:
        condition: trigger
        id: On
      sequence:
        - type: turn_on
          device_id: 20b995602s83et23b564926e3bf776d97cd
          entity_id: light.wand_lamp_1
          domain: light
    default: []
mode: single

You could also use templates and trigger variables to create a much more compact automation, but it has to be done using the yaml editor.

alias: New Automation
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.test
    for: '00:00:03'
condition: [] 
action:
  - service: light.turn_{{ trigger.to_state.state }}
    entity_id: light.wand_lamp_1
mode: single

EDIT: I inadvertently left out a few colons, I believe they are all fixed now

Thanks, both work.
very difficult syntax though. any websites explaining the basic commands and syntax like , for next loop, do while and if then else, declaring variables and other basic stuff?

Script/Automation Syntax

Variables

The first one in the post above is a good one, but you’ll need to review the docs for triggers and conditions to get the full picture. It’s not really a scripting language, though, and there are options like PyScript and AppDaemon if you’d prefer writing scripts and automations in Python instead.

I moved from domoticz some years agao . I missed Lua scripting. I was very uncomfotable with the bizare HA scripts . At first I used AppDaemon, then by using the templates switches , I began to understand the strange Jinja2 syntax. The developer tools are your friend. Look at the examples in the template and services tab.

After much persuasion , I have switched all my scripts to NodeRed . I thought it was like Blocky in domoticz . I was very wrong . It’s a simple elegant solution , perfectly adapted to the triggered nature of HA.

The learning curve is steep but woth it . Best of luck

1 Like

Thanks for the suggestion. I installed it but can’t start it. Get 502 bad gateway error

try this Node Red for Beginners - 2021 Edition (Using Home Assistant) - YouTube