Python Script - Determine printer state

Here is the python script. It checks the wattage registered by a smart outlet and depending on the wattage in use, makes a guess as to what is going on with the printer.

sensor= data.get('sensor')
logger.info("printer={}, sensor={}".format(prt_power,sensor))
prt=prt_power

printer_state=hass.states.get(prt)

logger.info("printer_state={}".format(printer_state.state))

wattage=float(printer_state.state)
logger.info("wattage={}".format(wattage))

# These values may need to be changed depending on your printer
# and should probably be moved to the automation yaml file.
if wattage>100:
  e_state="Pre-Heat hotend"
elif wattage > 69:
  e_state="Pre-Heat bed"
elif wattage > 50:
  e_state="Printing"
elif wattage > 2:
  e_state="Stand By"
elif wattage > 0:
  e_state="Unknown"
else:
  e_state="Off"

logger.info("Printer State={}".format(e_state))

hass.states.set(sensor,e_state)

Here is my automation.yaml entry. I run it every minute. I would actually like to only run it once a minute if there is wattage being registered by the outlet, but my automation skills aren’t that good.

- alias: fsocto3d_status
  trigger:
    platform: time 
    minutes: '/1'
    seconds: 0
  action:
    service: python_script.fsocto3d_script
    data: 
      printer: sensor.fsocto3d_pwr_power
      sensor: sensor.fsocto3d
2 Likes

The issue can be readily solved at some point by simply restarting your equipment. Simply switch off your printer, Epson Printer in Error State and laptop for about one minute, then leave them. Turn them on after that to verify if the mistake is resolved or not.