Triger after on -> on state change possible?

Hi,

I’m moving from another platform (Domoticz) and am really impressed what I can do with HA.
After some steep learning of the automation/scripting and finding a lot of info in this forum and the documentation things work pretty good. But now I’m a bit stuck and can’t find the answer, so here is my problem I hope someone knows how to solve:

To be able to fully move over I would like to have the same functionality I created there to trigger stuff based on remote controls that can sent not only “on”/“off” commands, but also multiple “on” or “off” sequences if you just press the corresponding buttons multiple times. With an automation you can then use this to not have on/off functionality, but also basic dimming.

I now have connected my other system to HA using MQTT and everything works fine if it sends “on” -> “off” state changes, or vice versa. But when I press the remote “on” button 2x times the messages are sent via MQTT to HA, but HA does not seem to register an “update” to the “on” state. So triggering of my script that should handle this does not work.

Question is: how can I get a trigger out of a entity that (via MQTT) gets updated but does not change it’s binary state?

Background how this should work:

The needs to catch the events from a (“KlikAanKlikUit”) remote that I use to dim my lights in steps: with each press of the “on” button on the remote the light need to be turned up by 20% until 100% and then cycles back to 20%. The “off” button, just turns the lights off, and if the lights are “off”, the first press of the “on” button just turns them on.

In this way you can use cheap/simpe remotes that do not support dimming, but only “on” and “off” to control a dimmable light.

I have the automation working in HA by manually triggering, but I can not get it to work to have it triggered by “on” -> “on” events.

You can do the brightness change by templating the light.turn_on service call.
About the multiple on/off button presses - I guess you’re using this integration?
I’m not too familiar with it sadly.

Try going to the Developer Tools - Events, paste button_pressed and click “Start listening”. Now, press your on button multiple times, does something show up?

The scripting is not so much the problem, I have indeed solved that with templating. And that script works if I just execute it manually.

I’m not using the hardware integration yet, as the remote signals are still being received by Domoticz using an RFX-COM receiver. I have set it up to forward this using MQTT and configured an MQTT switch in HA to get the data into HA.

If I look at the state of this MQTT switch in the developer tools I can see it changing state from “on” to “off” and vice versa, but not from “on” to “on” or from “off” to “off”. Also in the Logbook I see only on -> off events.

So it looks like on -> on and off -> off events are not recorded in HA. Or is that only For MQTT-based switches?

Btw: this is the script, that - as said - works great if triggered manually:

choose:
  - conditions:
      - condition: state
        entity_id: switch.remote_button_1
        state: 'off'
    sequence:
      - service: light.turn_off
        target:
          entity_id: light.hall
  - conditions:
      - condition: state
        entity_id: light.hall
        state: 'off'
    sequence:
      - service: light.turn_on
        target:
          entity_id: light.hall
default:
  - service: light.turn_on
    data_template:
      brightness: >
        {% set n = states.light.hall.attributes.brightness + 50 %}
        {% if n > 255 %}
          50
        {% else %}
          {{ n }}
        {% endif %}
    entity_id: light.hall

Then you should use a MQTT Trigger instead of the state change.

That doesn’t seem to work, just adding this as a trigger:

 trigger:
  - platform: mqtt
    topic: domoticz/out/32

Probably because the MQTT message is not only “on” or “off” it is a Json containing a number of key/value pairs, where “nvalue: 1” or “nvalue: 0” indicates if the button was pushed “on” or “off”.

So therefore this is defined as a switch like this:

  - platform: mqtt
    unique_id: 32
    name: "Remote_button_1"
    icon: mdi:remote
    state_topic: "domoticz/out/32"
    value_template: "{% if value_json.nvalue == 1 -%}ON{%- else %}OFF{%- endif %}"
    command_topic: "domoticz/in"
    payload_on: '{"command":"switchlight","idx":32,"switchcmd":"On"}'
    payload_off: '{"command":"switchlight","idx":32,"switchcmd":"Off"}'
    state_on: "ON"
    state_off: "OFF"
    optimistic: false

That works perfectly to get the state of the switch correct (although: as said it doesn’t trigger anything when receiving “on” while already “on”).

But it does not do anything if I create a trigger for it like this:

  trigger:
  - platform: mqtt
    topic: domoticz/out/32
    payload: '{% if value_json.nvalue == 1 -%}ON{%- else %}OFF{%- endif %}'

Maybe a solution like this? :thinking:

Are you referring to leaving out the payload check on the trigger and then checking the state in the condition? That’s what I actually did with the first example:

 trigger:
  - platform: mqtt
    topic: domoticz/out/32

Which then is the trigger to the script I posted earlier, that checks the condition using templating:

choose:
  - conditions:
      - condition: state
        entity_id: switch.remote_button_1
        state: 'off'
    sequence:
      - service: light.turn_off
        target:
          entity_id: light.hall
  - conditions:
      - condition: state
        entity_id: light.hall
        state: 'off'
    sequence:
      - service: light.turn_on
        target:
          entity_id: light.hall
default:
  - service: light.turn_on
    data_template:
      brightness: >
        {% set n = states.light.hall.attributes.brightness + 50 %}
        {% if n > 255 %}
          50
        {% else %}
          {{ n }}
        {% endif %}
    entity_id: light.hall

But as said: the second part works perfectly if triggered “manually”, but the trigger condition is never met.

No, try adding the MQTT trigger, so it triggers everytime a button is pressed, no matter if on or off, then add a condition, like the one I’ve linked, that checks the MQTT payload, so that your automation only executes the actions when you press the “on” button

Mmm, I don’t understand isn’t this a MQTT trigger?

Maybe I’m not getting the whole picture here :crazy_face:
The domoticz publishes on domoticz/out/32 everytime you press the on button, no matter what you’ve pressed before, correct?

That is correct :wink:

Alright. Where does it publish when you press the off button? Also on /out/32?

correct, again :wink:

On button:

domoticz/out/32 {
	"Battery" : 255,
	"RSSI" : 6,
	"description" : "",
	"dtype" : "Lighting 2",
	"hwid" : "2",
	"id" : "087C866",
	"idx" : 32,
	"name" : "Remote_button_1",
	"nvalue" : 1,
	"stype" : "AC",
	"svalue1" : "15",
	"switchType" : "On/Off",
	"unit" : 3
}

Off button pressed:

domoticz/out/32 {
	"Battery" : 255,
	"RSSI" : 7,
	"description" : "",
	"dtype" : "Lighting 2",
	"hwid" : "2",
	"id" : "087C866",
	"idx" : 32,
	"name" : "Remote_button_1",
	"nvalue" : 0,
	"stype" : "AC",
	"svalue1" : "15",
	"switchType" : "On/Off",
	"unit" : 3
}

As you can see, only the “nvalue” changes. But if you press the same button twice you get 2 identical messages.

I only see a difference in the RSSI, isn’t the nvalue 1 in both or am I blind? :crazy_face:

Oops, your eyes are good, my copy paste skill are bad :wink:
I updated the message…

1 Like

phew, good to know.
Try using the MQTT trigger you’ve used before.
Now add this condition:

condition:
    condition: template
    value_template: "{{ trigger.payload_json['nvalue'] == 1 }}"

This should now only trigger when pressing the on button, duplicate the automation and use 0 instead of 1 for the “off” version.
Once you get that working, we can take a look at the service calls :slight_smile:

1 Like

Wow. I now works! I had placed a leading space in the MQTT topic name, so it never triggered. :flushed:

Now it does. And it works perfectly with the script I posted earlier.

Thanks for your perseverance, really appreciated!

1 Like