MQTT sensor with "String" variable that I would like to use with Lovelace History-Graph

I have a doorbell which sends the string ‘ding’ over MQTT whenever the doorbell is depressed. I would like to simply display this with a Lovelace History-graph so that I can begin to understand when the doorbell is being pressed. <my use case is actually a road alarm that monitors my 3/4 mile long driveway and warns me when undesirables might be approaching>

The doorbell is defined as:
sensor:
- platform: mqtt
name: “Doorbell”
state_topic: “doorbell”
expire_after: 10

Can I convert the MQTT text ‘ding’ into a new sensor value?

You could use an mqtt binary sensor instead of an mqtt sensor (to convert it to on/off state).

binary_sensor:
  - platform: mqtt
    name: Doorbell
    state_topic: "doorbell"  # is that really your state topic?
    payload_on: 'ding'
    off_delay: 2  ### 2 seconds after switching on it will switch off as you have no 'off' payload.

This will give you a history graph like this (with green 2 second “on” stripes every time the doorbell is pressed):

Now if you want to see how many times it is pressed an hour/day/week use this sensor to define a history statistic sensor (type: count):

e.g.

sensor:
  - platform: history_stats
    name: Doorbell dings per hour
    entity_id: binary_sensor.doorbell
    state: 'on'
    type: count
    duration: 01:00:00

This will give you a history line graph of dings/hour.

Change the sensor names from Doorbell to “Driveway Monitor” or whatever you wish.

Tom_I, you made my day! Can’t wait too implement this tonight!!

Thanks!

Perhaps I’m missing something with the binary sensor- but it’s not changing state. I have it implemented as you suggest above.

My ESP8266 which is connected to the driveway alarm has the following code to publish “ding” when the alarm activates.
const char *mqtt_client_name = "Doorbell";
void getDoorBell()
{
if(digitalRead(doorBellPin) == 1 && alreadyTriggered == false)
{
client.publish(“doorbell”, “Ding”);
alreadyTriggered = true;
timer.setTimeout(6000, resetTrigger);
}
}

void resetTrigger()
{
  alreadyTriggered = false;
}

That’s all Greek to me. Though this:

client.publish(“doorbell”, “Ding”);

points towards a problem with letter case. Try this:

binary_sensor:
  - platform: mqtt
    name: Doorbell
    state_topic: "doorbell"  
    payload_on: 'Ding'  ### <------ note the capital 'D'
    off_delay: 2

It would be handy if you monitored the mqtt broker with a sniffer like mqttfx to see wha the actual message is. Subscribing to # will show all messages.

Indeed you are correct tom_I. Correcting the payload to match the case of that transmitted by the Arduino did the trick.

I was unaware of the sniffer- but I now see that this is a must for troubleshooting anything more complex.

Thanks again!

Playing with this solution a bit further, I would like to graphically indicate whenever this sensor activates during the day. For me, this might be an indication to check my security cameras for example. The reason that I would like this in graphical form is so that I can review at a glance if any mid-day events occur while I’m away.

It seems that I could use the history-graph or sensor card (and include a graph) in lovelace BUT my binary sensor has no value and my MQTT message is a text string. Any thoughts? I’m considering re-configuring the ESP8266 to send a message of “1” which I would like to then read as a value. I assume though that I’m not doing this in an elegant way.

As an aside, how about an automation that notifies you when the sensor trips and sends you a snapshot from your security camera(s)? During the day only if you like, or at any time.

If you set the mqtt binary sensor up as I outlined above then it has an on/off value. And the history graph for it will be a red bar with green stripes at times the sensor turned on. These also have timestamps you can see when you hover you mouse over it. If you also set up the history statistics sensor it will be a line graph with peaks at the times the sensor trips. The height of the peaks will be the number of trips per hour. You could set this up for any resolution you want (trips/5minutes for example). Either of these should give you enough information to look at your camera history.

OK- I have issue with the history-stats component… I should have mentioned this. For some reason the config is incorrect.

# this give stats on how often the driveway alarm sounds
  - platform: history_stats
    name: 'Vehicle_Traffic'
    entity_id: binary_sensor.driveway_monitor
    state: 'on'
    type: count
    duration: 01:00:00

binary_sensor:
  - platform: mqtt
    name: 'Driveway Monitor'
    state_topic: "doorbell"  
    payload_on: 'Ding'
    off_delay: 2  ### 2 seconds after switching on it will switch off as you have no 'off' payload.

It’s because I forgot to include an end time as well as a duration ( you have to provide exactly 2 of start , end and duration .) Try this:

  - platform: history_stats
    name: 'Vehicle_Traffic'
    entity_id: binary_sensor.driveway_monitor
    state: 'on'
    type: count
    end: '{{ now() }}'
    duration: 01:00:00

It should give the number of events in the last hour.

That was definitely the problem- and the solution.
I’m trying out both the history-graph and sensor cards in lovelace to see which one works best for my situation.

I would very much like to begin integrating the security cameras into my HA installation. Currently, I use a second RPI running MotioneyeOS to capture images based upon motion. I can get a good license plate photo with my setup and have been surprised at times by the random people that have come down my road.

You can create an automation that:

  1. triggers when binary_sensor.driveway_monitor is on
  2. takes a camera snapshot
  3. notifies you with the snapshot as an attachment.

You need to get your cameras into HA, possibly using the mpeg camera component or this addon:

You also need to be able to access HA outside your local network if you want to get the notifications while away. Use DuckDNS or Tor or a VPN.

You also need a notification service set up. What type of phone do you have?

I use an iphone and can get camera photo notifications like this:

- id: lane_gate_notification
  alias: 'Lane Gate Notification'
  trigger:
    platform: state
    entity_id: binary_sensor.lane_gate_filtered  ### trigger when the gate is opened or closed
  action:
  - service: camera.snapshot ### grab a camera snapshot
    data:
      entity_id: camera.rear
      filename: '/config/www/gate.jpg'
  - service: notify.all_ios_devices ### sent it to me
    data_template:
      message: >
        {% if is_state('binary_sensor.lane_gate_filtered', 'on') %}
          The laneway gate is open.
        {% else %}
          The laneway gate is closed.
        {% endif %}
      data:
        push:
          sound: >
            {% if is_state('binary_sensor.lane_gate_filtered', 'on') %}
              GateOpen.wav
            {% else %}
              GateClosed.wav
            {% endif %}
        attachment:
          url: https://mydomain.duckdns.org/local/gate.jpg
          content-type: jpg
          hide-thumbnail: false

The wife and I are both using iphone so, getting the notifications should work.
So many questions… . can I buy you a beer?
Are you using a Wifi camera by your gate? IF so, do you have an outside wifi antenna for your router? Just being nosy but, how do you get your ‘Lane Gate Notification’? I’m using this product, modified with an arduino: https://www.amazon.com/Dakota-Alert-DCPA-2500-Cycle-Probe/dp/B00443BDHS

1 Like

I’m using a wired IP camera mounted under the eaves of my house. It has a good view of the gate as it is not that far a way.

The sensor on the gate is a NodeMCU-32S board running ESPhome. Solar powered with a little 3.5W panel and an 18650 lithium battery.

Very nice implementation! I like how you used that limit switch and solar / battery combo. We have very similar use cases with the exception of my ability to provide a good wifi signal at the gate.

Thanks again for your help getting my issues fixed. I have both versions of the graph running and should hopefully capture all of the activity that occurs today.

UPDATE:

This is the history graph showing activity of my binary sensor. I’m getting very interested in this for logic / automation troubleshooting. I’m very excited to see what else might be an option in the upcoming releases of Lovelace!

image