How do I chart when three things are true?

When I go to bed, three things are true.

*Sensor.inbed says YES.

*Sensor.phonecharger says AC

*Sensor.lamp says OFF

I want to record what time all of these things become true (for the first time of one night, aka my bedtime) and then chart what time I went to bed over the course of a week.

With automations, I was able to get all the conditions to work properly. But, I wasn’t able to figure out what action I needed and how to use it.

One way is this:

Have your entities as triggers for an automation. This mean that if any of these entries have their state change to the required state then the automation will trigger.

Then, have all your entities as conditions too. This means that all the states must be as you require - even though any of the entities could trigger the automation.

The actions of your automation will then run when the last entity enters the required state.

At this time the last entity to enter the required state will trigger the automation and all the conditions will pass. Your actions will be executed.

Hi Templeton,

I was able to figure out how to make the condition part of the automation.

The hard part was the action of the automation. What do I make the automation DO to accomplish what I want?

One way is this:

When the automation runs it’s actions you could do this:

Set a date time helper to the current time and turn on a toggle helper.

If the automation runs again you need to check the toggle helper and only store the time if the toggle is off.

At some time before the next night you’ll need to turn off the toggle helper.

Hi Templeton,

Set a date time helper to the current time and turn on a toggle helper.

If the automation runs again you need to check the toggle helper and only store the time if the toggle is off.

How exactly do you do these things? I’m assuming you mean “call service” → “home assistant generic toggle”, but what do I target with this? How do you set a date-time helper to the current time?

Use a triggered template sensor.

configuration.yaml

template:
  - trigger:
      - platform: template
        value_template: >
          {{ is_state('sensor.inbed', 'YES') and
             is_state('sensor.phonecharger', 'AC') and
             is_state('sensor.lamp','OFF') }}
    sensor:
      - name: "Bed Time"
        icon: "mdi:bed-clock"
        device_class: timestamp
        state: "{{ now() }}"

Note that I have used the sensor entity ids and states you provided in your post. Please check that these are correct in Developer Tools → States and adjust if necessary.

In particular the lamp is probably a light entity so will actually be is_state('light.bedside_lamp','off') or something similar. The upper or lower case letters shown in the state in the developer tool is important.

1 Like

I am confused. Is this thread actually about a data visualization question (the “how to chart”), or is this about data recording (the “how to I feed the time stamp of my bedtime to HA”)…?

Or both?

(… and if I read it correctly, NOT an automation trigger / condition question… right?)

@blank321 you might want to edit your question (or questions), because it wasn’t clear… to me anyways.

It’s both:

I have supplied a solution to the first part. For the second part, charting times can be tricky. The only way to do it using core cards is with a strip chart:

Untitled

There may be a way with apexcharts or some other third party tool but I don’t actually know of one that will plot a time against a weekday.

Template sensor. You can have it use a simple template that is true only if all conditions are true. Then you have an entity that tells you how long you have been sleeping per night.

Actually if you want the duration in bed, rather than the time you went to bed, you can use a binary and template sensor you can feed that to the history stats sensor to get the time in bed each day:

template:
  - binary_sensor:
      - name: "Bed Occupied"
        icon: "mdi:bed"
        state: >
          {{ is_state('sensor.inbed', 'YES') and
             is_state('sensor.phonecharger', 'AC') and
             is_state('sensor.lamp','OFF') }}

Then in sensors.yaml:

- platform: history_stats
  name: Time in Bed
  entity_id: binary_sensor.bed_occupied
  state: "on"
  type: time
  end: "{{ today_at(12:00) }}" # midday to midday, change this if you need to.
  duration:
    hours: 24

This duration can be much more easily plotted. You can use Apexcharts’s maximum and group by day.

1 Like

Hi Everybody, thank you so much for all of your help. This is BOTH a data visualization and a data-recording problem.

Here is the template binary sensor I’m using as a test.

  - binary_sensor:
        unique_id: trynasleep
        state: >
          {{
            is_state('binary_sensor.withings_in_bed_name', 'Detected')
            and
            is_state('sensor.sm_g998u_charger_type', 'ac')
          }}
        # Optional, sometimes one of the sensors may very briefly report the wrong value, this just prevents false triggers.
        #delay_on:
        #  seconds: 10

Rhe binary sensor that is supposed to report when “I’m in bed” and “my phone is plugged in” does not go to “on” or “active” or whatever means it is working properly. It only shows “off” and a few bits of unavailable, despite the history for in-bed and phone-charging history showing both of these things worked.

image

I will be trying the alternative approaches to the problem/sensor code that was posted in the thread overnight when I get back from work, AND trying to fix the current code. Thank you all

The state name is not Detected. It is on.