Help to optimize rules & additional questions (easy newbie questions)

Dear community,

I’m testing several home autmation systems - also on my bucket list is “Home Assistant”, which was set up and running within a few minutes. It looks quite good to me and I tried to implement my “test scenario”.

There are two shellies: shelly1 which acts as switch and a shelly pm1 which acts as power meter, both connected via mqtt broker (external mosquito). Goal: If the power meter ist above 1 it should turn on the switch and if below 1 it should turn off the switch. I ended up with these automation rules:

- alias: 'rule1'
  trigger:
    platform: numeric_state
    entity_id: sensor.powermeter
    above: 1
  action:
    - entity_id: switch.shellytest
      service: switch.turn_on

	  
	  
	  
- alias: 'rule2'
  trigger:
    platform: state
    entity_id: sensor.powermeter
    below: 1
  action:
    - entity_id: switch.shellytest
      service: switch.turn_off

This works like expected.

My questions are:

  1. Do I really need two rules for that? Can I use one, but how? I was wondering if I could do a equals zero comparison to check if there is power or not and do a simple if power == 0 then turn switch off else turn switch on.

  2. I realised that these triggers only fires when the numeric state changes, but not on every incoming mqtt message. Said that if shelly pm1 sends 100 x sending power 0 there is no firing of the trigger. I.e. in openhab you have a “item changed” trigger. Is there something similar in Home Assistant?

Many thanks for your time and I hope to find someone who can answer my questions.

best regards
Matt

  1. no, but the way to combine them into one automation is not straight forward. It looks something like:
- alias: 'rule'
  trigger:
    platform: state
    entity_id: sensor.powermeter
  action:
    - entity_id: switch.shellytest
      service: >
        {% if trigger.to_state.state | int >= 1 %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}

or you could shorten it a bit:

- alias: 'rule'
  trigger:
    platform: state
    entity_id: sensor.powermeter
  action:
    - entity_id: switch.shellytest
      service: switch.turn_{{ 'on' if trigger.to_state.state | int >= 1 else 'off' }}

I haven’t tested those templates but I’m pretty sure they will work.

But sometimes it’s better to just use two automations until you get your “HA legs”.

  1. you can use the trigger above (leaving out the normally added “to:” option for a state trigger) that will fire not only on every state change but also every attribute change. But then you need to implement some other type of program control to stop it from performing the action on every state and attribute change…unless of course that’s what you intend.

Dear finity

thank you very much for your precise and clear answer. This brings really light into the darkness - after playing around for some hours testing HA syntax and HA logic, this helped me a lot to go on with my research

I will do some further testing with your suggestions.

best regards
Matt

1 Like

Also just as an FYI…

unless you add my name (with the @ symbol in front of it) or you click on the “reply” button in my post I won’t see your replies unless I look at my unread topics, which sometimes I don’t get to frequently. But if I see a notification I’ll open those first.

@finity: Perfect. I will remember it for the future. Thanks again for your kind help.

1 Like