Publish all states to MQTT

I’m sure I found a script in the forums previously that published all states to MQTT. I cannot seem to find it and didn’t bookmark it. Can anyone point me in the right direction please?

As a start check out the script in the Templates tab on the demo https://home-assistant.io/demo/ would only require a bit of modification.

{% for state in states.sensor -%}
  {%- if loop.first %}The {% elif loop.last %} and the {% else %}, the {% endif -%}
  {{ state.name | lower }} is {{state.state}} {{- state.attributes.unit_of_measurement}}
{%- endfor -%}.

Eventsream Component publishes all state to mqtt

How?

I see from instructions that links 2 HA .

I actually want all topic for one installation instead

It just uses standard mqtt message
To receive this message just subscribe to specific topic

For example:
HA on Pi use “hass_main” as publish topic
To monitor events you subscribe to “hass_main”

You are not required to Subscribe or Publish.
This component allows 1way communication.
In this case you only send out(publish).

You can test this by enabling component to publish.
Use cell phone app or other client to Subscribe to message and verify result

2 Likes

sorry, I am too newb to understand.

Which state are recorded? I think all, correct?

Ok, so now which is the name of the topic for each state recorded?

So if I put this code (by the way what do I put instead of Myservername , my host name?? Like raspberrypi ?)

mqtt_eventstream:
  publish_topic: MyServerName

then how are the topic called?

If I have a switch.power_plug or a light light.table_lamp

the topics are??

Or am I missing something

No problem. I’m being a little lazy in answering to be honest so I might not be clear.

Publish and Subscribe topics have no meaning.
You can make them whatever you want but make it something that make sense. I would use the locations name in this case.

What you have listed will work. Just remember
Publish=send
Subscribe=receive

Any device you want to receive should subscribe to “MyServerName”
I am not sure exact message format but believe it’s something like what’s here https://home-assistant.io/developers/websocket_api/

Sorry, but I do not get it.

I tell you my goal: my goal is to have a topic for each state.

if I have a light

light.table_lamp

I have a STATE (on or off) and STATE ATTRIBUTE (colour, brightness, and so on); same for all light and switch in HASS.

Is it possible to publish MQTT Topics with the STATE (and STATE ATTRIBUTE) ?

Does

mqtt_eventstream:
  publish_topic: MyServerName

create topics of all STATES of HASS, or not?

All is one topic with each sensor individually broken out in topic

Another HA instance will show individual sensor state.

If using generic MQTT client you need to parse the individual sensor
I believe messages will be like below with every sensor reporting this to topic at each state change

{
   "id": 18,
   "type":"event",
   "event":{
      "data":{
         "entity_id":"light.bed_light",
         "new_state":{
            "entity_id":"light.bed_light",
            "last_changed":"2016-11-26T01:37:24.265390+00:00",
            "state":"on",
            "attributes":{
               "rgb_color":[
                  254,
                  208,
                  0
               ],
               "color_temp":380,
               "supported_features":147,
               "xy_color":[
                  0.5,
                  0.5
               ],
               "brightness":180,
               "white_value":200,
               "friendly_name":"Bed Light"
            },
            "last_updated":"2016-11-26T01:37:24.265390+00:00"
         },
         "old_state":{
            "entity_id":"light.bed_light",
            "last_changed":"2016-11-26T01:37:10.466994+00:00",
            "state":"off",
            "attributes":{
               "supported_features":147,
               "friendly_name":"Bed Light"
            },
            "last_updated":"2016-11-26T01:37:10.466994+00:00"
         }
      },
      "event_type":"state_changed",
      "time_fired":"2016-11-26T01:37:24.265429+00:00",
      "origin":"LOCAL"
   }
}

I guess that you were looking for this:

Just following up on this as I had a similar question - the export component is probably great if you want exporting via whitelisting certain components for export.

MQTT Eventstream (as mentioned upthread) is probably best for what its intended - connecting two home assistant instances together.

The original poster seems to want something different - for which MQTT statestream may be a better fit (it sends state changes out on a hierarchy of topics that mirrors the sensor hierarchy with a configurable graft point).