Trigger via MQTT Range

Hey Guys -

I recently set up an ESP32 which monitors and sends how many amps it measures to an MQTT topic once a second. I’m trying to figure out the best way for HA to turn toggle the power on a separate device based on what range the amp value is in.

Example:
If the payload of officeac/amps is > 0.06, turn on light1.
If the payload of officeac/amps is < 0.5, turn off light1

I tried setting this up in Automations, but seemingly could only use static values as the trigger.

Any suggestions? Thanks!

Wouldn’t two Numeric State Triggers meet your requirements?

alias: example
trigger:
  - id: 'on'
    platform: numeric_state
    entity_id: sensor.officeac
    above: 0.06
  - id: 'off'
    platform: numeric_state
    entity_id: sensor.officeac
    below: 0.5
condition: []
action:
  - service: "light.turn_{{ trigger.id }}"
    target:
      entity_id: light.light1

Or is the issue that you want to be able to base the values of above and below on other entities?

Think that’ll work - thanks a ton!

If you happen to know and don’t mind, what would I use for the action if I wanted to publish to MQTT as the action. For instance if I wanted to publish the payload “on” to the topic “sensor.officeacvariable” if the value was above 0.5 and “off” if below 0.06?

Appreciate it!

There is a service to publish to mqtt, and any service can be an action in an automation.

These requirements are opposite of what you specified in your first post. Is that intentional or a mistake?

Hey - Sorry - I had them backwards in the initial post :frowning:

alias: example
trigger:
  - id: 'on'
    platform: numeric_state
    entity_id: sensor.officeac
    above: 0.5
  - id: 'off'
    platform: numeric_state
    entity_id: sensor.officeac
    below: 0.06
condition: []
action:
  - service: mqtt.publish
    data:
      topic: your/mqtt/topic
      payload: "{{ trigger.id" }}"

Reference: mqtt.publish

Thanks - Really appreciate your help!

You’re welcome!

Please consider marking my post above with the Solution tag. Only you, the topic’s author, can mark one post to represent the solution. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

Hopefully in future ChatGPT will learn only from the solution tagged posts :wink:

2 Likes