Check if state entity exists...?

I have a mqtt payload that gets sent over to home-assistant but if i restart home-assistant the mqtt doesn’t get sent over, so i’d like to create a sensor for it if it’s on or off
so far i’ve created this…? does this look correct?

  - platform: template
    sensors:
      mqtt_ring_alarm:
        value_template: '{% if states.alarm_control_panel.ring_alarm ==  none %}off{% else %}{{ states.alarm_control_panel.ring_alarm }}{% endif %}'

this works!!! awesome…, anyway to change the output if it’s “on”
it prints this out,

<template state alarm_control_panel.ring_alarm=disarmed; code_format=None, changed_by=None, friendly_name=Ring Alarm @ 2019-04-11T22:47:29.895275-07:00>off

How can i go about restarting the service (once) if it’s set to off?
if it reports back false (if off) and True (if on), next i’d like to create an automation to restart the service

1 Like

There may be a shortcut you could explore:

Create an automation that:

not quite sure how to go from the triggered when HA starts, can you provide an example?

Whatever is publishing the sensor’s state to the broker should do so using retain=true. It instructs the broker to retain (store) it. When Home Assistant restarts, it re-connects to the broker, re-subscribes to the sensor’s topic and immediately receives the retained state.

All of my MQTT sensors and binary sensors publish their state as retained messages. When Home Assistant restarts, all sensor values are received from the broker.


Don’t confuse this with retain: true in the configuration of Home Assistant’s MQTT Sensor and Binary Sensor components. I’m talking about the physical sensor device you’re using that publishes its state to the broker. That device ought to publish its state as a retained message.

im not a developer and wouldn’t know where to suggest this in someone’s code

Tell me more about the device that is sending the payload.

https://github.com/tsightler/ring-alarm-mqtt/blob/master/README.md, code: https://github.com/tsightler/ring-alarm-mqtt/blob/master/ring-alarm-mqtt.js

If you’re uncomfortable with editing Javascript, just ask the developer to add an option to config.js that would allow you to specify retain: true.

If you’re not squeamish about modifying Javascript, the change is trivial.

The script relies on MQTT.js version 2.18.8 and uses mqtt.publish which is documented here.

The script’s author uses only one publishing option: qos. We will add the retain option.

Change this line from this:

mqttClient.publish(topic, message, { qos: 1 })

to this:

mqttClient.publish(topic, message, { qos: 1, retain: true })

That should do the trick.

1 Like

I will give it a try thanks! do i need to edit my mqtt config in home-assistant too?

Off the top of my head, no. The change you’re making doesn’t alter connections or topics. It only specifies that a message (published by the script) must now be retained by the broker.

so far no go :frowning: added the line and rebooted the services, then rebooted home-assistant

For the retain flag to become effective you have to wait for the first MQTT message to be sent from the device.

hmm. ok
why doesn’t this work?

  - platform: template
    sensors:
      mqtt_ring_alarm:
        friendly_name: "Ring Alarm"
        value_template: > 
          {% if is_state("states.alarm_control_panel.ring_alarm", 'on') %}
          {% else %}
            Off
          {% endif %}

Change that to

          {% if is_state("alarm_control_panel.ring_alarm", 'on') %}

When you use is_state the first parameter is just the entity id.

I see at least two errors:

  • is_state requires an entity_id and a state.
  • the value_template should always return a valid state.

I fixed the first error but I don’t know what you want to report as a state when alarm_control_panel.ring_alarm is on.

        value_template: > 
          {% if is_state('alarm_control_panel.ring_alarm', 'on') %}
            ??? What goes here ???
          {% else %}
            Off
          {% endif %}

well it doesn’t show up at all, hence unknown… trying to make a sensor if there’s no entity states in home assistant

To fix problems with MQTT, it’s helpful to use an MQTT client like MQTT Explorer. It allows you to view all your broker’s topics, each topic’s payload, and if the payload is retained by the broker.

Have you read exxamalte’s explanation?