Post sensor values to MQTT

As described in one of my earlier posts.

Try this experiment:

  • Go to Developer Tools > Events
  • Scroll down to Listen to an event
  • Enter call_service in Event to subscribe to
  • Click Start Listening
  • In another browser tab (or window) open the Lovelace UI and turn on a light.
  • Return to the browser pane containing Developer tools > Events and you’ll see the call_service event details for the service that turned on the light (i.e. light.turn_on).

For example, I have a patio light and here’s the event reported when I turned on the light:

Event 0 fired 3:10 PM:

{
    "event_type": "call_service",
    "data": {
        "domain": "light",
        "service": "turn_on",
        "service_data": {
            "entity_id": "light.patio"
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-03-10T19:10:16.090753+00:00",
    "context": {
        "id": "redacted",
        "parent_id": null,
        "user_id": "redacted"
    }
}

I can use that information to create an automation with an Event Trigger that fires only for that specific service call. Something like this:

  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: light
      service: turn_on

For your application, I imagine it would look like this:

  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: speedtestdotnet
      service: speedtest
1 Like

@123, Thanks for the help.

The final solution is this (from the automation UI);

Trigger

platform: event
event_type: state_changed
event_data:
  domain: speedtestdotnet
  attributes: last_updated
  entity_id: sensor.speedtest_download

Action

data_template:
  payload_template: '{{ states.sensor.speedtest_download.state }}'
  topic: emon/speedtest
service: mqtt.publish
3 Likes

I think you missed the spirit of what constitutes the “Solution”.

Have I? In what way?

The answer will reveal itself to you eventually.

Thank’s. This is the easiest way I found :slight_smile:

1 Like

You just posted in a thread that had been dead for over a year and I have no idea what you’re talking about.

Thank you @baz123 for posting your solution here :slight_smile:

2 Likes

Just for the record, another example that gets triggered by updates on sensor:

  - alias: Publish on sensor update
    trigger:
      platform: state
      entity_id: sensor.0x123456000123456d_temperature
    action:
      - service: mqtt.publish
        data_template:
          payload_template: "{{ states('sensor.0x123456000123456d_temperature') }}"
          topic: whatever/topic/you/want
2 Likes

Just for the record, there’s no need to use payload_template. Even the example I posted over a year ago simply uses payload with a template.

A year later and now it’s no longer required to use data_template either. It was deprecated in favor of simply using data (several versions ago).

On core-2021.3.3 replacing payload_template by payload gives an error.

Same applies for data_template vs data.

Upgrade Home Assistant.

2021.3 lacks many enhancements and any recent discussion on this forum is focused on the features of recent versions, not old ones. 2021.3 is now four versions ago.

In fact, the official documentation always represents features found in the latest version of Home Assistant. Anyone using an old version may experience difficulties with the documentation because it will explain something that may not exist, or work differently, in their old version.

I have this working great for sending my cars entity’s to mqtt also
But 1 of the entity’s is having extra attributes, How do I get the tribute on the mqtt as well?

mqtt_statestream:
  base_topic: bmw
  include:
    entities:
      - sensor.i4_edrive40_remaining_battery_percent
      - sensor.i4_edrive40_mileage
      - binary_sensor.i4_edrive40_charging_status
      - device_tracker.i4_edrive40
      - sensor.i4_edrive40_charging_end_time
      - sensor.i4_edrive40_remaining_range_total
      - sensor.i4_edrive40_remaining_range_electric
      - binary_sensor.i4_edrive40_lids
      - sensor.i4_edrive40_charging_status
      - binary_sensor.i4_edrive40_connection_status

for example the entity device_tracker.i4_edrive40 has attributes like

Source type: gps
Latitude: 53.73
Longitude: 3.3
GPS accuracy: 0
Direction: 165

How do I get these attributes also on mqtt
i have tried
device_tracker.i4_edrive40.Latitude for example, but this does not work
any tips?

Please help me to understand mqtt a little better. I have this enphase sensor for solar production.

sensor.envoy_122248025029_today_s_energy_production