Count hours based on two conditions

Hello,

I currently count the ours my electric car is charging, and from that calculate how much money I spend on charging:

I use history_stats:

  - platform: history_stats
    name: Charge ON eGolf last 30 days
    entity_id: switch.wvwzzzauzgw901946_charging
    state: 'on'
    type: time
    end: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration:
      days: 30

And template:

  kwh_egolf_30days:
    friendly_name: 'Kwh eGolf Last 30 days'
    value_template: '{{ ((((states.sensor.charge_on_egolf_last_30_days.state | round (2) ) * ( 2300 | round(2$
    unit_of_measurement: "kWh"

  nok_egolf_30days:
    friendly_name: 'NOK eGolf Last 30 days'
    value_template: '{{ ((states.sensor.kwh_egolf_30days.state | round (2) ) * (0.925 | round(2) )) | round(2$
    unit_of_measurement: "NOK"

This works fine, however I can charge the car at my workplace, hence it would be nice to get the calculation to only calculate the cost of charging at home. I have entity for device tracker for the car but I dont know how to count the hours based on car being at home and charging. Any input would be greatly apprechiated!

Thanks!

Might have found a solution:

binary_sensor:
      - platform: template
        sensors:
          golf_charge_at_home:
            value_template: >-
              {{ is_state('device_tracker.wvwzzzauzgw901946', 'home')
                 and is_state('switch.wvwzzzauzgw901946_charging', 'on') }}

Will know if works by tommorow.

1 Like

That will work.

Just need to build that into your other sensors or make a new one. You could even calculate how much money you’re saving by charging elsewhere.